package samsung.sds.business.vdiManagementSystem.firewallManagement.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import samsung.sds.business.vdiManagementSystem.firewallManagement.dao.FirewallMapper;
import samsung.sds.business.vdiManagementSystem.firewallManagement.model.FirewallEntity;
import samsung.sds.business.vdiManagementSystem.firewallManagement.service.FirewallManagementService;
import samsung.sds.departmentManagement.model.DepartmentEntity;
import samsung.sds.departmentManagement.service.DepartmentManagementService;
import samsung.sds.system.common.baseQuery.AbstractBaseService;
import samsung.sds.system.common.baseQuery.BaseMapper;
import samsung.sds.system.common.baseQuery.model.PageData;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.system.utils.CommonTools;

/**
 * @author hongxin.luo
 * @createDate 2016-04-21
 * 
 * 防火墙管理Service实现类
 */
@Service
public class FirewallManagementServiceImpl extends
		AbstractBaseService<FirewallEntity> implements
		FirewallManagementService {
	@Autowired
	private FirewallMapper firewallMapper;
	
	@Autowired
	private DepartmentManagementService departmentManagementServiceImpl;

	@Override
	protected BaseMapper<FirewallEntity> getMapper() {
		return null;
	}
	
	/**
	 * 查询组织结构以及各部门的VDI信息实现
	 * departmentVDIEntitys：组织结构与部门VDI信息共存实
	 * tmpDepartmentVDIEntitys：部门VDI信息实体
	 * departmentEntitys：组织结构实体
	 */
	public List<DepartmentEntity> queryDepartmentVDI() {
		
		QueryParam param = new QueryParam();
		List<DepartmentEntity> departmentVDIEntitys  = new ArrayList<DepartmentEntity>();
		List<DepartmentEntity> tmpDepartmentVDIEntitys  = new ArrayList<DepartmentEntity>();
		List<DepartmentEntity> departmentEntitys = departmentManagementServiceImpl.queryList(param);
		
		for (DepartmentEntity departmentEntity : departmentEntitys) {
			
			departmentVDIEntitys.add(departmentEntity);
			
			tmpDepartmentVDIEntitys = firewallMapper.selectDEPT_VDI(departmentEntity.getDeptId());
			
			for (DepartmentEntity tmpDepartmentEntity : tmpDepartmentVDIEntitys) {
				
				departmentVDIEntitys.add(tmpDepartmentEntity);
			}
		}
		return departmentVDIEntitys;
	}
	
	/**
	 * 分页查询VDI下的所有防火墙信息实现
	 * @param param
	 * @return tmpPageData
	 */
	public PageData<FirewallEntity> queryFirewallInVDI(QueryParam param) {
		List<FirewallEntity> list = firewallMapper.queryFirewallInVDIListPage(param);
		PageData<FirewallEntity> tmpPageData = new PageData<FirewallEntity>();
		tmpPageData.setQueryResult(list);
		tmpPageData.setQueryParam(param);
		return tmpPageData;
	}
    /**
     * 删除VDI下的防火墙信息
     * @param lastModifyUser,firewalls
     */
	public int updateFirewall(String lastModifyUser,String[] firewalls) {
		return firewallMapper.updateVDI_FIREWALL(lastModifyUser,firewalls);
	}
	
	/**
	 * 返回查询的部门树与vdi信息.
	 * @return
	 */
	public Map<String, List<?>> getTreeData() {
		Map<String, List<?>> map = new HashMap<String, List<?>>();
		map.put("dept", departmentManagementServiceImpl.queryList(new QueryParam()));
		map.put("vdiIP", firewallMapper.findVDIIP());
		
		return map;
	}

	public void batchInserFireWall(List<FirewallEntity> list) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		List<FirewallEntity> curFireWallList = new ArrayList<FirewallEntity>();
		for(int i = 0; i < list.size(); i++) {
			for(int j = 0; j < list.get(i).getVdiInfoList().size(); j++) {
				FirewallEntity fe = new FirewallEntity();
				fe.setId(CommonTools.getKeyUUID());
				fe.setCreateTime(sdf.format(new Date()));
				fe.setLastModifyTime(sdf.format(new Date()));
				fe.setCreateUser(CommonTools.getSessionUserName());
				fe.setLastModifyUser(CommonTools.getSessionUserName());
				
				fe.setDirection(list.get(i).getDirection());
				fe.setEndTime(list.get(i).getEndTime());
				fe.setPort(list.get(i).getPort());
				fe.setProtocol(list.get(i).getProtocol());
				fe.setRemark(list.get(i).getRemark());
				fe.setStartTime(list.get(i).getStartTime());
				fe.setTargetIp(list.get(i).getTargetIp());
				fe.setVdiIP(list.get(i).getVdiInfoList().get(j).getIp());
				fe.setVdiId(list.get(i).getVdiInfoList().get(j).getId());
				
				curFireWallList.add(fe);
			}
		}
		
		firewallMapper.batchInsert(curFireWallList);
	}

	public void updateFirewallEntity(FirewallEntity firewallEntity) {
		firewallMapper.updateFirewallEntity(firewallEntity);
	}
	public List<FirewallEntity> queryFirewallEntityByVDIId(String vdiId) {
		return firewallMapper.queryFirewallEntityByVDIId(vdiId);
	}

	@Override
	public FirewallEntity getFireWallByID(String id) {
		return firewallMapper.getFireWallByID(id);
	}

	public int batchUpdateFirewallEntity(List<FirewallEntity> firewallEntitys) {
		return firewallMapper.batchUpdateFirewallEntity(firewallEntitys);
	}

	@Override
	public int insertFirewallInfo(FirewallEntity fe) {
		fe.setId(CommonTools.getKeyUUID());
		fe.setCreateUser(CommonTools.getSessionUserName());
		fe.setLastModifyUser(CommonTools.getSessionUserName());
		fe.setVdiId(fe.getVdiInfoList().get(0).getId());
		fe.setVdiIP(fe.getVdiInfoList().get(0).getIp());
		
		return firewallMapper.insertFireWallInfo(fe);
	}
}
